home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / full / nt / MSSql / I386 / sqlx86.exe / PTK / SAMPLES / OLEAUTO / GETNPV / NPVTRIG.SQL < prev    next >
Encoding:
Text File  |  1996-04-03  |  2.2 KB  |  82 lines

  1. if exists (select * from sysobjects where id = object_id('dbo.NPVtestdata'))
  2.     drop table NPVTestdata
  3. go
  4. create table NPVTestdata
  5.     (project smallint identity primary key,
  6.      cost decimal (12,2),
  7.      retrate decimal (5,4),
  8.      pmt0 decimal (12,2),
  9.      pmt1 decimal (12,2),
  10.      pmt2 decimal (12,2),
  11.      pmt3 decimal (12,2),
  12.      pmt4 decimal (12,2),
  13.      NPV  decimal (12,2) NULL)
  14. go
  15.  
  16. create trigger GetNPV_trigger on NPVTestdata for INSERT as
  17.     declare @pObj int, @hr int
  18.     declare @source varchar(30)
  19.     declare @desc varchar(200)
  20.     declare @project smallint
  21.     declare @cost decimal (12,2)
  22.     declare @retrate decimal (5,4)
  23.     declare @pmt0 decimal (12,2)
  24.     declare @pmt1 decimal (12,2)
  25.     declare @pmt2 decimal (12,2)
  26.     declare @pmt3 decimal (12,2)
  27.     declare @pmt4 decimal (12,2)
  28.     declare @NPV decimal (12,2)
  29.  
  30.  
  31.     /* if only one record inserted by the command, don't open a cursor */
  32.     if @@rowcount = 1
  33.     begin
  34.         exec @hr=sp_OACreate "GetNPV.CGetNPV", @pObj OUT
  35.         if @hr <> 0 goto Err
  36.  
  37.         select @project=project, @cost=cost, @retrate=retrate, 
  38.             @pmt0=pmt0, @pmt1=pmt1, @pmt2=pmt2, @pmt3=pmt3, @pmt4=pmt4 
  39.             from inserted
  40.         exec @hr=sp_OAMethod @pObj, "GetNPV", @NPV OUT, 
  41.             @cost, @retrate, @pmt0, @pmt1, @pmt2, @pmt3, @pmt4
  42.         if @hr <> 0 goto Err
  43.     
  44.         update NPVtestdata set NPV=@NPV where project=@project
  45.     end
  46.     else
  47.     /* if multiple rows inserted by command (INSERT...SELECT) use a cursor */
  48.     begin
  49.         exec @hr=sp_OACreate "GetNPV.CGetNPV", @pObj OUT
  50.         if @hr <> 0 goto Err
  51.  
  52.         declare c1 cursor for select project, cost, retrate, 
  53.             pmt0, pmt1, pmt2, pmt3, pmt4 from inserted
  54.         open c1
  55.  
  56.         while (1=1)
  57.         begin
  58.             fetch c1 into @project, @cost, @retrate, 
  59.                 @pmt0, @pmt1, @pmt2, @pmt3, @pmt4
  60.             if @@fetch_status <> 0 break
  61.  
  62.             exec @hr=sp_OAMethod @pObj, "GetNPV", @NPV OUT, 
  63.                 @cost, @retrate, @pmt0, @pmt1, @pmt2, @pmt3, @pmt4
  64.             if @hr <> 0 goto Err
  65.     
  66.             update NPVtestdata set NPV=@NPV where project=@project
  67.  
  68.         end
  69.         deallocate c1
  70.     end
  71.  
  72.     goto Done
  73.  
  74. Err:
  75.     raiserror ('Error in calculating Net Present Value. ', 16,1)
  76.     exec sp_OAGetErrorInfo null, @source OUT, @desc OUT
  77.     select hr=convert(binary(4),@hr), source=@source, description=@desc
  78.     rollback transaction
  79.  
  80. Done:
  81.     exec sp_OADestroy @pObj
  82. go